package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.exception.ServiceException;
import com.mall.mapper.GoodsStockMapper;
import com.mall.pojo.GoodsStockPo;
import com.mall.service.GoodsStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.vo.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author zhangshiqi
 * @since 2020-09-13
 */
@Service
public class GoodsStockServiceImpl extends ServiceImpl<GoodsStockMapper, GoodsStockPo> implements GoodsStockService {

    @Autowired
    private GoodsStockMapper goodsStockMapper;

    /**
     * 分页查询库存数据
     */
    @Override
    public PageObject<GoodsStockPo> doFindPageObject(String stockName, Integer pageCurrent) {
        //1.参数校验
        if (pageCurrent == null || pageCurrent < 1)
            throw new IllegalArgumentException("页码值不正确");
        //2.PageHelper
        Page<Object> page = PageHelper.startPage(pageCurrent, 5);
        QueryWrapper<GoodsStockPo> queryWrapper = new QueryWrapper<>();
        if (stockName != null) {
            queryWrapper.like("name", stockName);
        }
        List<GoodsStockPo> goodsStockPoList = goodsStockMapper.selectList(queryWrapper);
        //3.封装结果
        return new PageObject<>(page.getTotal(), goodsStockPoList, (long) page.getPages(), 5, pageCurrent);
    }

    /**
     * 删除库存数据
     */
    @Override
    public void doDeleteObjects(String ids) {
        //1.参数校验
        if (ids == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.解析参数
        String[] id = ids.split(",");
        //3.语句执行
        int rows = 0;
        for (String s : id) {
            rows += goodsStockMapper.deleteById(s);
        }
        if (rows == 0)
            throw new ServiceException("库存数据删除失败");
    }

    /**
     * 新增库存数据
     */
    @Override
    public void doAddObjects(GoodsStockPo goodsStockPo) {
        //1.参数校验
        if (goodsStockPo == null)
            throw new IllegalArgumentException("数据不正确");
        if (StringUtils.isEmpty(goodsStockPo.getSpecifications()))
            throw new IllegalArgumentException("商品规格不能为空");
        if (goodsStockPo.getPrice() <= 0)
            throw new IllegalArgumentException("商品销售价格不能小于等于0");
        //2.添加默认值
        goodsStockPo.setAlertValue((int) (goodsStockPo.getStock() * 0.15));
        //3.语句执行
        int rows = goodsStockMapper.insert(goodsStockPo);
        if (rows == 0)
            throw new ServiceException("库存添加失败");
    }

    /**
     * 修改库存数据
     */
    @Override
    public void doUpdateObjects(GoodsStockPo goodsStockPo) {
        //1.参数校验
        if (goodsStockPo == null)
            throw new IllegalArgumentException("数据不正确");
        if (StringUtils.isEmpty(goodsStockPo.getSpecifications()))
            throw new IllegalArgumentException("商品规格不能为空");
        if (goodsStockPo.getPrice() <= 0)
            throw new IllegalArgumentException("商品销售价格不能小于等于0");
        //2.语句执行
        int rows = goodsStockMapper.updateById(goodsStockPo);
        if (rows == 0)
            throw new ServiceException("没有该库存数据");
    }

    /**根据ID查询库存数据*/
    @Override
    public GoodsStockPo doFindGoodsStockById(String id) {
        //1.参数校验
        if (id==null)
            throw new IllegalArgumentException("请先选择要更改的数据");
        //2.返回数据
        return goodsStockMapper.selectById(id);
    }
}
